#include "goods.h"
#include "ui_goods.h"

#include <QMessageBox>
#include <QSqlError>



goods::goods(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::goods)
{
    ui->setupUi(this);
    setWindowTitle("仓库货物管理");

    model1= new QSqlRelationalTableModel(this);
    model1->setTable
            ("goods");
    model1->select();
    model1 -> setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
    model1->setHeaderData(0,Qt::Horizontal,tr("商品号"));
//    model1->setHeaderData(1,Qt::Horizontal,tr("类别号"));
    model1->setHeaderData(1,Qt::Horizontal,tr("商品名"));
    model1->setHeaderData(2,Qt::Horizontal,tr("价格"));
    model1->setHeaderData(3,Qt::Horizontal,tr("数量"));

    ui->tableView->setModel(model1);

    model2 = new QSqlRelationalTableModel(this);
    model2 ->setTable
            ("goods");
    model2 ->select();
    model2 -> setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
    model2->setHeaderData(0,Qt::Horizontal,tr("商品号"));
    model2->setHeaderData(1,Qt::Horizontal,tr("类别号"));
    model2->setHeaderData(1,Qt::Horizontal,tr("商品名"));
    model2->setHeaderData(2,Qt::Horizontal,tr("价格"));
    model2->setHeaderData(3,Qt::Horizontal,tr("数量"));

    ui->tableView_2->setModel(model2);
    ui->num_out->setMaximum(INT_MAX);
    ui->num_in->setMaximum(INT_MAX);
}

goods::~goods()
{
    delete ui;
}

void goods::on_SearchAll_clicked()
{
    model1->setTable("goods");
    model1->select();
    model1->setHeaderData(0,Qt::Horizontal,tr("商品号"));
    model1->setHeaderData(1,Qt::Horizontal,tr("商品名"));
    model1->setHeaderData(2,Qt::Horizontal,tr("价格"));
    model1->setHeaderData(3,Qt::Horizontal,tr("数量"));

}

void goods::on_SearchAll_2_clicked()
{
    model2->setTable("goods");
    model2->select();
    model2->setHeaderData(0,Qt::Horizontal,tr("商品号"));
    model2->setHeaderData(1,Qt::Horizontal,tr("商品名"));
    model2->setHeaderData(2,Qt::Horizontal,tr("价格"));
    model2->setHeaderData(3,Qt::Horizontal,tr("数量"));

}

void goods::on_add_clicked()
{
    int addrow = model1->rowCount();
    int id = addrow+1;
    model1->insertRow(addrow);
    model1->setData(model1->index(addrow,0),id);
    model1->submitAll();
}

void goods::on_delete_2_clicked()
{
    int currow = ui->tableView->currentIndex().row();//确定行数
    if(currow==-1)
    {
        QMessageBox::warning(this,tr("删除提醒"),
                                            tr("请选中商品所在行再点击删除"),
                                               QMessageBox::Yes);
    }
    else
    {
        model1->removeRow(currow);

        int issure = QMessageBox::warning(this,tr("删除提醒"),
                                    tr("确认要删除当前行吗？"),
                                       QMessageBox::Yes,QMessageBox::No);
         if(issure==QMessageBox::No){
         model1->revertAll();
        }
         else{
            model1->submitAll();
         }
    }
}



void goods::on_ok_clicked()
{
    model1->database().transaction();
       if(model1->submitAll()){
           if(model1->database().commit()){
               QMessageBox::information(this,"提交提醒","修改成功");
           }
       }
       else{
           model1->database().rollback();
           QMessageBox::information(this,"提交提醒",tr("提交错误")
            .arg(model1->lastError().text()),QMessageBox::Yes);
       }
}

void goods::on_search_clicked()
{
    int i = ui->comboBox->currentIndex();
    QString str = ui->lineEdit->text();
    switch (i) {
    case 0:
        model1->setFilter(QString("id = '%1'").arg(str));
        model1->select();
        break;
    case 1:
        model1->setFilter(QString("name = '%1'").arg(str));
        model1->select();
        break;
    }
}

void goods::on_search_2_clicked()
{
    int i = ui->comboBox_2->currentIndex();
    QString str = ui->lineEdit->text();
    switch (i) {
    case 0:
        model2->setFilter(QString("id = '%1'").arg(str));
        model2->select();
        break;
    case 1:
        model2->setFilter(QString("name = '%1'").arg(str));
        model2->select();
        break;
    }
}

void goods::on_revoke_clicked()
{
    model1->revertAll();
}

void goods::on_refresh_clicked()
{
    model2->setTable("goods");
    model2->select();
    model2->setHeaderData(0,Qt::Horizontal,tr("商品号"));
    model2->setHeaderData(1,Qt::Horizontal,tr("商品名"));
    model2->setHeaderData(2,Qt::Horizontal,tr("价格"));
    model2->setHeaderData(3,Qt::Horizontal,tr("数量"));
}

void goods::on_uploadPicture_clicked()
{
    QString picturename = QFileDialog::getOpenFileName(this, "选择商品图片", ".", "Image File(*.png *.jpg *.jpeg *.bmp)");
    if (picturename.isEmpty()) return;
    myPicImg.load(picturename);
    ui->prePictureLabel->setPixmap(QPixmap::fromImage(myPicImg));
}

void goods::on_instock_clicked()
{
    QSqlQuery query;
    query.exec(QString("select categoryid from category where name='%1'").arg(ui->Category_in->currentText()));
    query.next();
    int categoryid = query.value(0).toInt();//将要入库的商品类别
    QString Name = ui->name_in->text();//商品名称
    double inputprice = ui->inprice->text().toDouble();//进价
//    double outputprice = ui->newOutputPriceLineEdit->text().toFloat();//售价
    int count = ui->num_in->value();//入库量
    query.exec(QString("insert into goods(categoryid, name, picture, price, num) values(%1, '%2', NULL, %3, %4)").arg(categoryid).arg(Name).arg(inputprice).arg(count));//插入新入库的商品记录
       //插入照片
    QByteArray picdata;
    QBuffer buffer(&picdata);
    buffer.open(QIODevice::WriteOnly);
    myPicImg.save(&buffer, "JPG");
    QVariant var(picdata);
    QString sqlstr = "update goods set picture=? where name='" + Name + "'";
    query.prepare(sqlstr);
    query.addBindValue(var);
    if(!query.exec())
    {
        QMessageBox::information(0, QObject::tr("提示"), "入库失败，请检测连接");
    }
    else
    {
        QMessageBox::information(0, QObject::tr("提示"), "入库成功");
    }
       //刷新网格信息

    model1= new QSqlRelationalTableModel(this);
    model1->setTable
            ("goods");
    model1->select();
    model1 -> setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
    model1->setHeaderData(0,Qt::Horizontal,tr("商品号"));
//    model1->setHeaderData(1,Qt::Horizontal,tr("类别号"));
    model1->setHeaderData(1,Qt::Horizontal,tr("商品名"));
    model1->setHeaderData(2,Qt::Horizontal,tr("价格"));
    model1->setHeaderData(3,Qt::Horizontal,tr("数量"));

    ui->tableView->setModel(model1);
}
